﻿using Hopeless.Metaphysical;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCS.XNA.Utils;
using UCS.XNA.Utils.Animation;

namespace Hopeless.Physical
{
    internal class Sword : IScalable
    {
        private const int WIDTH = 2;
        private const int HEIGHT = 15;

        private Texture2D _texture;
        
        private Rectangle _space;
        public Rectangle Space
        {
            get { return _space; }
        }

        private Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set
            {
                _position = value;
                this.InitializeSpace();
            }
        }

        public Vector2 RelativeOrigin
        {
            get { return new Vector2(WIDTH / 2f, 0); }
        }

        private float _scale = 1f;
        public float Scale
        {
            get { return _scale; }
            set
            {
                _scale = value;
                this.InitializeSpace();
            }
        }

        private float _facingAngle = 0;
        public float FacingAngle
        {
            get { return _facingAngle; }
            set
            {
                _facingAngle = value;
                if (_facingAngle > Math.PI * 2)
                    _facingAngle = (float)(Math.PI * 2 - _facingAngle);
                else if (_facingAngle < 0)
                    _facingAngle = (float)(Math.PI * 2 + _facingAngle);
            }
        }

        private bool _swinging;
        public bool Swinging
        {
            get { return _swinging; }
        }

        private List<Zombie> _zombiesHit = new List<Zombie>();
        private float _damage;
        private float _knockbackForce;

        public void Initialize()
        {
            _texture = GameResources.Content.Load<Texture2D>("Sword");
            this.InitializeSpace();
        }

        private void InitializeSpace()
        {
            _space = new Rectangle((int)_position.X, (int)_position.Y, (int)(WIDTH * _scale), (int)(HEIGHT * _scale));
        }

        public void Update(GameTime gameTime)
        {
            if (_swinging)
            {
                foreach (var zombie in HopelessGame.Universe.CurrentWave.SpawnedZombies)
                {
                    var colliding = Physics.CheckRotatedRectanglesCollision(_space, this.RelativeOrigin, _facingAngle, zombie.BodySpace, zombie.RelativeOrigin, zombie.FacingAngle);
                    if (colliding && !_zombiesHit.Contains(zombie))
                    {
                        zombie.TakeDamage((int)_damage);
                        var ang = _facingAngle + Math.PI;
                        var cos = System.Math.Cos(ang);
                        var sin = System.Math.Sin(ang);
                        var direction = new Vector2((float)cos, (float)sin);

                        Physics.ForceAnimations.Add(new ForceTranslateAnimation(zombie, direction, _knockbackForce));
                        _zombiesHit.Add(zombie);
                    }
                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            GameResources.SpriteBatch.Begin();

            GameResources.SpriteBatch.Draw(_texture, _space, null, Color.White, _facingAngle, new Vector2(_texture.Width / 2, 0), SpriteEffects.None, 0);

            GameResources.SpriteBatch.End();
        }

        public void Swing(float damage, float knockbackForce)
        {
            _swinging = true;
            _damage = damage;
            _knockbackForce = knockbackForce;
        }

        public void StopSwing()
        {
            _swinging = false;
            _zombiesHit = new List<Zombie>();
        }
    }
}
